﻿using Capstone.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Capstone.Models;
using Capstone.Services;
using Capstone.Properties;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Capstone.Views;
using System.Net.Http;

namespace Capstone.Views
{
    public partial class GutSymptomDiaryPage : ContentPage
    {
        public List<int> DateOptions;
        public List<string> FrequencyOptions;
        public List<string> SymptomOptions;
        public List<string> OtherSymptomOptions;
        public List<string> SleepSymptomOptions;
        

        public GutSymptomDiaryPage()
        {
            
            InitializeComponent();

            BindingContext = new GutSymptomDiaryViewModel();
            
            DateOptions = AddDateOptions();
            FrequencyOptions = AddDiaryFrequency();

            SymptomOptions = AddSymptoms();

            OtherSymptomOptions = AddOtherSymptoms();

            SleepSymptomOptions = AddSleepSymptoms();

            DiaryLength.ItemsSource = DateOptions;
            DiaryFrequency.ItemsSource = FrequencyOptions;

            SymptomList.ItemsSource = SymptomOptions;
            SymptomList.SelectedIndices = new List<int> {  };

            OtherSymptomList.ItemsSource = OtherSymptomOptions;
            OtherSymptomList.SelectedIndices = new List<int> { };

            SleepSymptomList.ItemsSource = SleepSymptomOptions;
            SleepSymptomList.SelectedIndices = new List<int> { };

        }


        List<int> AddDateOptions()
        {
            List<int> options = new List<int>();
            for (int i = 1; i < 366; i++)
            {
                options.Add(i);
            }
            return options;
        }

        List<string> AddDiaryFrequency()
        {
            List<string> options = new List<string>();
            options.Add(Properties.Resources.GutFreq1);
            options.Add(Properties.Resources.GutFreq2);
            options.Add(Properties.Resources.GutFreq3);
            return options;
        }

        List<string> AddSymptoms()
        {
            List <string> options = new List<string>();
            options.Add(Properties.Resources.GutSymptom1);
            options.Add(Properties.Resources.GutSymptom2);
            options.Add(Properties.Resources.GutSymptom3);
            options.Add(Properties.Resources.GutSymptom4);
            options.Add(Properties.Resources.GutSymptom5);
            options.Add(Properties.Resources.GutSymptom6);
            return options;
        }

        List<string> AddOtherSymptoms()
        {
            List<string> options = new List<string>();
            options.Add(Properties.Resources.OtherSymptom1);
            options.Add(Properties.Resources.OtherSymptom2);
            options.Add(Properties.Resources.OtherSymptom3);
            options.Add(Properties.Resources.OtherSymptom4);
            return options;
        }

        List<string> AddSleepSymptoms()
        {
            List<string> options = new List<string>();
            options.Add(Properties.Resources.SleepSymptom1);
            options.Add(Properties.Resources.SleepSymptom2);
            options.Add(Properties.Resources.SleepSymptom3);
            return options;
        }
        private async void SaveFunction(object sender, EventArgs e)
        {
            if (DiaryName.Text == null || DiaryName.Text == "" || DiaryLength.SelectedIndex == -1 || DiaryFrequency.SelectedIndex == -1 || (string.IsNullOrWhiteSpace(SymptomList.Text) && string.IsNullOrWhiteSpace(OtherSymptomList.Text) && string.IsNullOrWhiteSpace(SleepSymptomList.Text)) || part_emails.Text == "" || part_emails.Text == null)
            {
                ErrorMessage.IsVisible = true;
            }
            else
            {
                Item newItem = new Item()
                {
                    Id = Guid.NewGuid().ToString(),
                    Text = DiaryName.Text,
                    Description = Properties.Resources.GutDescription,
                    Length = DiaryLength.SelectedIndex + 1,
                    Frequency = (string)DiaryFrequency.SelectedItem,
                    Symptoms = SymptomList.Text.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries),
                    OtherSymptoms = OtherSymptomList.Text.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries),
                    SleepSymptoms = SleepSymptomList.Text.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries),
                    startDate = startDateX
                };
                await DependencyService.Get<IDataStore<Item>>().AddItemAsync(newItem);

                int customInt = 0;
                HttpClient client = new HttpClient();

                string data = "[{\"record_id\":\"" + DiaryName.Text + " (ID:" + GetHashCode() + ")" + "\", \"gut_symptom_diary_name\":\"" + DiaryName.Text + "\"";
                data += ", \"gut_diary_time\":\"" + startDateX.Date.Year + "-" + startDateX.Date.Month + "-" + startDateX.Date.Day + "\"";
                data += ", \"gut_symptom_diary_length\":\"" + (DiaryLength.SelectedIndex + 1) + "\"";
                data += ", \"gut_symptom_diary_frequency\":\"" + (DiaryFrequency.SelectedIndex + 1) + "\"";

                Console.WriteLine("Tämä on pituus " + newItem.Symptoms.Length);

                string symptoms = SymptomList.Text;

                if (symptoms.Contains(Properties.Resources.GutSymptom1))
                {
                    data += ", \"gut_symptom_diary_symptoms___1\":\"" + "1" + "\"";
                    customInt++;
                }
                else data += ", \"gut_symptom_diary_symptoms___1\":\"" + "0" + "\"";
                if (symptoms.Contains(Properties.Resources.GutSymptom2))
                {
                    data += ", \"gut_symptom_diary_symptoms___2\":\"" + "1" + "\"";

                    customInt++;
                }
                else data += ", \"gut_symptom_diary_symptoms___2\":\"" + "0" + "\"";
                if (symptoms.Contains(Properties.Resources.GutSymptom3))
                {
                    data += ", \"gut_symptom_diary_symptoms___3\":\"" + "1" + "\"";

                    customInt++;
                }
                else data += ", \"gut_symptom_diary_symptoms___3\":\"" + "0" + "\"";
                if (symptoms.Contains(Properties.Resources.GutSymptom4))
                {
                    data += ", \"gut_symptom_diary_symptoms___4\":\"" + "1" + "\"";

                    customInt++;
                }
                else data += ", \"gut_symptom_diary_symptoms___4\":\"" + "0" + "\"";
                if (symptoms.Contains(Properties.Resources.GutSymptom5))
                {
                    data += ", \"gut_symptom_diary_symptoms___5\":\"" + "1" + "\"";

                    customInt++;
                }
                else data += ", \"gut_symptom_diary_symptoms___5\":\"" + "0" + "\"";
                if (symptoms.Contains(Properties.Resources.GutSymptom6))
                {
                    data += ", \"gut_symptom_diary_symptoms___6\":\"" + "1" + "\"";

                    customInt++;
                }
                else data += ", \"gut_symptom_diary_symptoms___6\":\"" + "0" + "\"";


                Console.WriteLine(symptoms.Count());
                Console.WriteLine(customInt);
                if (newItem.Symptoms.Length > customInt)
                {
                    if (newItem.Symptoms.Length - customInt == 1)
                    {
                        
                            data += ", \"gut_symptom_diary_symptoms___7\":\"" + "1" + "\"";
                    }

                    if (newItem.Symptoms.Length - customInt == 2)
                    {
                            data += ", \"gut_symptom_diary_symptoms___7\":\"" + "1" + "\"";
                            data += ", \"gut_symptom_diary_symptoms___8\":\"" + "1" + "\"";
                    }

                    if (newItem.Symptoms.Length - customInt == 3)
                    {
                            data += ", \"gut_symptom_diary_symptoms___7\":\"" + "1" + "\"";
                            data += ", \"gut_symptom_diary_symptoms___8\":\"" + "1" + "\"";
                            data += ", \"gut_symptom_diary_symptoms___9\":\"" + "1" + "\"";
                    }

                    if (newItem.Symptoms.Length - customInt == 4)
                    {
                            data += ", \"gut_symptom_diary_symptoms___7\":\"" + "1" + "\"";
                            data += ", \"gut_symptom_diary_symptoms___8\":\"" + "1" + "\"";
                            data += ", \"gut_symptom_diary_symptoms___9\":\"" + "1" + "\"";
                            data += ", \"gut_symptom_diary_symptoms___10\":\"" + "1" + "\"";
                    }
                }

                string othersymptoms = OtherSymptomList.Text;
                customInt = 0;

                if (othersymptoms.Contains(Properties.Resources.OtherSymptom1))
                {
                    data += ", \"gut_symptom_diary_other_symptoms___1\":\"" + "1" + "\"";
                    customInt++;
                }
                else data += ", \"gut_symptom_diary_other_symptoms___1\":\"" + "0" + "\"";
                if (othersymptoms.Contains(Properties.Resources.OtherSymptom2))
                {
                    data += ", \"gut_symptom_diary_other_symptoms___2\":\"" + "1" + "\"";
                    customInt++;
                }
                else data += ", \"gut_symptom_diary_other_symptoms___2\":\"" + "0" + "\"";
                if (othersymptoms.Contains(Properties.Resources.OtherSymptom3))
                {
                    data += ", \"gut_symptom_diary_other_symptoms___3\":\"" + "1" + "\"";
                    customInt++;
                }
                else data += ", \"gut_symptom_diary_other_symptoms___3\":\"" + "0" + "\"";
                if (othersymptoms.Contains(Properties.Resources.OtherSymptom4))
                {
                    data += ", \"gut_symptom_diary_other_symptoms___4\":\"" + "1" + "\"";
                    customInt++;
                }
                else data += ", \"gut_symptom_diary_other_symptoms___4\":\"" + "0" + "\"";

                if (newItem.OtherSymptoms.Length > customInt)
                {
                    if (newItem.OtherSymptoms.Length - customInt == 1)
                    {

                        data += ", \"gut_symptom_diary_other_symptoms___5\":\"" + "1" + "\"";
                    }

                    if (newItem.OtherSymptoms.Length - customInt == 2)
                    {
                        data += ", \"gut_symptom_diary_other_symptoms___5\":\"" + "1" + "\"";
                        data += ", \"gut_symptom_diary_other_symptoms___6\":\"" + "1" + "\"";
                    }

                    if (newItem.OtherSymptoms.Length - customInt == 3)
                    {
                        data += ", \"gut_symptom_diary_other_symptoms___5\":\"" + "1" + "\"";
                        data += ", \"gut_symptom_diary_other_symptoms___6\":\"" + "1" + "\"";
                        data += ", \"gut_symptom_diary_other_symptoms___7\":\"" + "1" + "\"";
                    }

                    if (newItem.OtherSymptoms.Length - customInt == 4)
                    {
                        data += ", \"gut_symptom_diary_other_symptoms___5\":\"" + "1" + "\"";
                        data += ", \"gut_symptom_diary_other_symptoms___6\":\"" + "1" + "\"";
                        data += ", \"gut_symptom_diary_other_symptoms___7\":\"" + "1" + "\"";
                        data += ", \"gut_symptom_diary_other_symptoms___8\":\"" + "1" + "\"";
                    }
                    if (newItem.OtherSymptoms.Length - customInt == 5)
                    {
                        data += ", \"gut_symptom_diary_other_symptoms___5\":\"" + "1" + "\"";
                        data += ", \"gut_symptom_diary_other_symptoms___6\":\"" + "1" + "\"";
                        data += ", \"gut_symptom_diary_other_symptoms___7\":\"" + "1" + "\"";
                        data += ", \"gut_symptom_diary_other_symptoms___8\":\"" + "1" + "\"";
                        data += ", \"gut_symptom_diary_other_symptoms___9\":\"" + "1" + "\"";
                    }
                    if (newItem.OtherSymptoms.Length - customInt == 6)
                    {
                        data += ", \"gut_symptom_diary_other_symptoms___5\":\"" + "1" + "\"";
                        data += ", \"gut_symptom_diary_other_symptoms___6\":\"" + "1" + "\"";
                        data += ", \"gut_symptom_diary_other_symptoms___7\":\"" + "1" + "\"";
                        data += ", \"gut_symptom_diary_other_symptoms___8\":\"" + "1" + "\"";
                        data += ", \"gut_symptom_diary_other_symptoms___9\":\"" + "1" + "\"";
                        data += ", \"gut_symptom_diary_other_symptoms___10\":\"" + "1" + "\"";
                    }
                }

                string sleepsymptoms = SleepSymptomList.Text;
                customInt = 0;

                if (sleepsymptoms.Contains(Properties.Resources.SleepSymptom1))
                {
                    data += ", \"gut_symptom_diary_sleep_symptoms___1\":\"" + "1" + "\"";
                    customInt++;
                }
                else data += ", \"gut_symptom_diary_sleep_symptoms___1\":\"" + "0" + "\"";
                if (sleepsymptoms.Contains(Properties.Resources.SleepSymptom2))
                {
                    data += ", \"gut_symptom_diary_sleep_symptoms___2\":\"" + "1" + "\"";
                    customInt++;
                }
                else data += ", \"gut_symptom_diary_sleep_symptoms___2\":\"" + "0" + "\"";
                if (sleepsymptoms.Contains(Properties.Resources.SleepSymptom3))
                {
                    data += ", \"gut_symptom_diary_sleep_symptoms___3\":\"" + "1" + "\"";
                    customInt++;
                }
                else data += ", \"gut_symptom_diary_sleep_symptoms___3\":\"" + "0" + "\"";


                if (newItem.SleepSymptoms.Length > customInt)
                {
                    if (newItem.SleepSymptoms.Length - customInt == 1)
                    {

                        data += ", \"gut_symptom_diary_sleep_symptoms___4\":\"" + "1" + "\"";
                    }

                    if (newItem.SleepSymptoms.Length - customInt == 2)
                    {
                        data += ", \"gut_symptom_diary_sleep_symptoms___4\":\"" + "1" + "\"";
                        data += ", \"gut_symptom_diary_sleep_symptoms___5\":\"" + "1" + "\"";
                    }
                }

                data += ", \"part_emails\":\"" + part_emails.Text + "\"";

                data += "}]";

                var values = new Dictionary<string, string>
                {
                    { "token", "540E97741AA3EFF280E55B5C9DB1CD6D" },
                    { "content", "record" },
                    { "action", "import" },
                    { "format", "json" },
                    { "overwriteBehavior", "normal" },
                    { "forceAutoNumber", "false" },
                    { "data",  data},
                    { "returnContent", "count" },
                    {"returnFormat", "json" }
                };

                var content = new FormUrlEncodedContent(values);
                var response = await client.PostAsync("https://redcap.utu.fi/api/", content);
                var responseString = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseString);
                if (responseString.Contains("error"))
                {
                    Save_Fail.IsVisible = true;
                    Save_Success.IsVisible = false;
                }
                else
                {
                    Save_Success.IsVisible = true;
                    Save_Fail.IsVisible = false;
                }

                Console.WriteLine(data);

                await Navigation.PopAsync();
                await Navigation.PopAsync();

            }
            
        }
    }
}